/*
    4. Write a program in C to print individual characters of string in reverse order. 

    Test Data :
    Input the string : w3resource.com

    Expected Output :

    The characters of the string in reverse are :
    m  o  c  .  e  c  r  u  o  s  e  r  3  w
*/

#include <stdio.h>
#include <stdlib.h>

char* getStr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p = ch;
        p++;
    }
    *p = '\0';
    return str;
}

int getStrLen(char *str)
{
    int len = 0;
    char *p = str;
    while(*p != '\0'){
        len++;
        p++;
    }
    len++; // '\0'
    return len;
}

void reverseString(char *str)
{
    int len = getStrLen(str) - 1;
    char tmp;
    int i = 0, j = len - 1;
    while(i < j){
        tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
        i++;
        j--;
    }
}

int main(void)
{
    char *str = malloc(1024);
    int i = 0, len;
    printf("Input the string : ");
    getStr(str);
    reverseString(str);
    len = getStrLen(str);    
    printf("The characters of the string are :\n ");
    while(i < len - 1){
        printf("%c  ", str[i++]);
    }

    return 0;
}